load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_license//rules:license.bzl", "license")
load("//tools:cc.bzl", "COPTS")
load("//tools:transition.bzl", "PLATFORM", "transition_default_constraints")

####
# Due to the complexity of the build, the overlay is maintained independently.
# See https://github.com/sallustfire/jemalloc/tree/bcr-5.3.0?tab=readme-ov-file#bazel-build-of-jemalloc
####

package(default_applicable_licenses = [":license"])

license(
    name = "license",
    license_kinds = ["@rules_license//licenses/spdx:BSD-2-Clause"],
    license_text = "COPYING",
)

alias(
    name = "jemalloc",
    actual = ":jemalloc_with_defaults",
    visibility = ["//visibility:public"],
)

cc_library(
    name = "jemalloc_core",
    srcs = [
               "src/arena.c",
               "src/background_thread.c",
               "src/base.c",
               "src/bin.c",
               "src/bin_info.c",
               "src/bitmap.c",
               "src/buf_writer.c",
               "src/cache_bin.c",
               "src/ckh.c",
               "src/counter.c",
               "src/ctl.c",
               "src/decay.c",
               "src/div.c",
               "src/ecache.c",
               "src/edata.c",
               "src/edata_cache.c",
               "src/ehooks.c",
               "src/emap.c",
               "src/eset.c",
               "src/exp_grow.c",
               "src/extent.c",
               "src/extent_dss.c",
               "src/extent_mmap.c",
               "src/fxp.c",
               "src/hook.c",
               "src/hpa.c",
               "src/hpa_hooks.c",
               "src/hpdata.c",
               "src/inspect.c",
               "src/jemalloc.c",
               "src/large.c",
               "src/log.c",
               "src/malloc_io.c",
               "src/mutex.c",
               "src/nstime.c",
               "src/pa.c",
               "src/pa_extra.c",
               "src/pac.c",
               "src/pages.c",
               "src/pai.c",
               "src/peak_event.c",
               "src/prof.c",
               "src/prof_data.c",
               "src/prof_log.c",
               "src/prof_recent.c",
               "src/prof_stats.c",
               "src/prof_sys.c",
               "src/psset.c",
               "src/rtree.c",
               "src/safety_check.c",
               "src/san.c",
               "src/san_bump.c",
               "src/sc.c",
               "src/sec.c",
               "src/stats.c",
               "src/sz.c",
               "src/tcache.c",
               "src/test_hooks.c",
               "src/thread_event.c",
               "src/ticker.c",
               "src/tsd.c",
               "src/witness.c",
           ] + select({
               "//settings/flags:zone_allocator": ["src/zone.c"],
               "//conditions:default": [],
           }) +
           select({
               "//settings/compiler:msvc_compatible": [
                   "include/msvc_compat/C99/stdbool.h",
                   "include/msvc_compat/C99/stdint.h",
                   "include/msvc_compat/strings.h",
                   "include/msvc_compat/windows_extra.h",
               ],
               "//conditions:default": [],
           }) + select({
        "//settings/flags:cxx": ["src/jemalloc_cpp.cpp"],
        "//conditions:default": [],
    }),
    hdrs = ["//include/jemalloc"],
    copts = ["-Iinclude/jemalloc/internal"] + select({
        "//settings/compiler:msvc_compatible": [
            "-Iinclude/msvc_compat",
            "-Iinclude/msvc_compat/C99",
        ],
        "//conditions:default": [],
    }) + COPTS,
    includes = ["include"],
    linkopts = select({
        "@platforms//os:android": ["-ldl"],
        "@platforms//os:linux": [
            "-ldl",
            "-lpthread",
            "-lrt",
        ],
        "//conditions:default": [],
    }),
    local_defines = select({
        "//settings:jet": ["JEMALLOC_JET"],
        "//conditions:default": [],
    }) + select({
        "//settings:no_private_namespace": ["JEMALLOC_NO_PRIVATE_NAMESPACE"],
        "//conditions:default": [],
    }),
    tags = ["manual"],
    visibility = ["//:__subpackages__"],
    deps = ["//include/jemalloc/internal"],
)

transition_default_constraints(
    name = "jemalloc_with_defaults",
    src = ":jemalloc_core",
    platform = PLATFORM,
    settings = {},
)

# C_UTIL_INTEGRATION_SRCS from the Makefile which are shared with the integration testlib
cc_library(
    name = "utils",
    srcs = [
        "src/malloc_io.c",
        "src/nstime.c",
        "src/ticker.c",
    ],
    hdrs = ["//include/jemalloc"],
    copts = ["-Iinclude/jemalloc/internal"] + COPTS,
    includes = ["include"],
    linkopts = select({
        "@platforms//os:linux": [
            "-lpthread",
            "-lrt",
        ],
        "//conditions:default": [],
    }),
    linkstatic = True,
    tags = ["manual"],
    visibility = ["//:__subpackages__"],
    deps = ["//include/jemalloc/internal"],
)
